package com.example.quartz.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.quartz.common.Constant.CommonConstant;
import com.example.quartz.common.Response.Result;
import com.example.quartz.common.Response.ResultCode;
import com.example.quartz.common.Response.ResultTool;
import com.example.quartz.project.dto.QuartzDto;
import com.example.quartz.project.po.Quartz;
import com.example.quartz.project.service.QuartzService;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 打小就爱笑捏
 * @date 2019/11/11
 */
@Component
@RestController
@CrossOrigin
@RequestMapping(path = "/QuartzController")
public class QuartzController {

    @Autowired
    private QuartzService quartzService;
    @Resource
    private Scheduler scheduler;

    /**
     * 分页列表查询
     *
     * @param dto
     * @param page      分页
     * @param orderList 排序
     * @return
     */
    @GetMapping(value = "/getAllByPage")
    public ResponseEntity<Object> getAllByPage(QuartzDto dto, Page page, String orderList) {
        return new ResponseEntity<>(quartzService.getAllByPage(dto, page, orderList), HttpStatus.OK);
    }

    /**
     * 通过id查询定时任务
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/selectById/{id}")
    public Result<Object> queryById(@PathVariable String id) {
        Quartz quartzJob = quartzService.getById(id);
        return ResultTool.success(quartzJob);
    }

    /**
     * 添加定时任务
     *
     * @param quartz
     * @return
     */
    @PostMapping(value = "/addQuartz")
    public Result<Object> addQuartz(Quartz quartz) throws Exception {
        List<Quartz> list = quartzService.list(new QueryWrapper<Quartz>().eq("job_class_name", quartz.getJobClassName()));
        if (list != null && list.size() > 0) {
            return ResultTool.fail(ResultCode.QUARTZ_JOBCLASSNAME_IS_EXIST);
        }
        return ResultTool.success(quartzService.add(quartz));
    }

    /**
     * 更新定时任务
     *
     * @param quartzJob
     * @return
     */
    @PostMapping(value = "/update")
    public Result<?> update(Quartz quartzJob) throws Exception {
        try {
            quartzService.edit(quartzJob);
        } catch (SchedulerException e) {
            return ResultTool.fail(ResultCode.QUARTZ_UPDATE_IS_FAIL);
        }
        return ResultTool.success("更新定时任务成功");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteById/{id}")
    public Result<?> deleteById(@PathVariable String id) {
        Quartz quartzJob = quartzService.getById(id);
        if (ObjectUtils.isEmpty(quartzJob)) {
            return ResultTool.fail(ResultCode.QUARTZ_IS_NOT_EXIST);
        }
        quartzService.delete(quartzJob);
        return ResultTool.success("删除成功!");
    }


    /**
     * 暂停定时任务
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/stopJobById/{id}")
    public Result<Object> stopJobById(@PathVariable String id) throws Exception {
        Quartz job = null;
        try {
            job = quartzService.getBaseMapper().selectById(id);
            if (ObjectUtils.isEmpty(job)) {
                return ResultTool.fail(ResultCode.QUARTZ_IS_NOT_EXIST);
            }
            scheduler.pauseJob(JobKey.jobKey(job.getJobClassName().trim()));
        } catch (SchedulerException e) {
            throw new Exception("暂停定时任务失败");
        }
        job.setStatus(CommonConstant.STATUS_DISABLE);
        quartzService.updateById(job);
        return ResultTool.success("暂停定时任务成功!");
    }

    /**
     * 恢复定时任务
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/resumeJobById/{id}")
    public Result<Object> resumeJobById(@PathVariable String id) throws Exception {
        Quartz job = quartzService.getBaseMapper().selectById(id);
        if (ObjectUtils.isEmpty(job)) {
            return ResultTool.fail(ResultCode.QUARTZ_IS_NOT_EXIST);
        }
        quartzService.resumeJob(job);
        return ResultTool.success("恢复定时任务成功!");
    }


}
